/**
 *  excel summary 构建类
 *
 * created by nongzhizhong on 2023/02/13
 */
const BaseExcel = require('../../../adapter/excel');

class SummaryExcel extends BaseExcel {
  constructor() {
    super('rocket');
  }

  async generate(data, output) {
    this.addSheet('违反规则表');
    const columns = [
      {
        header: '编号',
        key: 'index',
        width: 10,
      },
      {
        header: '文件名',
        key: 'fileName',
        width: 16.93,
      },
      {
        header: '接口名',
        key: 'functionNames',
        width: 20,
      },
      {
        header: '代码行号',
        key: 'lines',
        width: 16.93,
      },
      {
        header: '规则说明',
        key: 'ruleName',
        width: 40,
      },
      {
        header: '规则编号',
        key: 'ruleCode',
        width: 30,
      },
      {
        header: '测试人员',
        key: 'userName',
        width: 16.93,
      },
      {
        header: '备注',
        key: 'description',
        width: 10,
      },
      {
        header: '缺陷状态',
        key: 'status',
        width: 10,
      },
    ];
    this.addRows(columns, data);
    // style
    const beginIndex = 'A'.charCodeAt();
    const endIndex = columns.length + beginIndex;
    const row = data.length + 1;
    for (let i = beginIndex; i < endIndex; i += 1) {
      for (let j = 1; j <= row; j += 1) {
        const position = `${String.fromCharCode(i)}${j}`;
        if (j === 1) {
          this.setCell(position, { font: { bold: true } });
        } else if ([67, 68, 69, 70].includes(i)) {
          this.setCell(position, {
            alignment: {
              vertical: 'middle',
              horizontal: 'center',
              wrapText: true,
            },
          });
        } else {
          this.setCell(position);
        }
      }
    }
    // write excel template file
    return this.writeFile(output);
  }
}
module.exports = SummaryExcel;
